package com.wangshouyu.mp.pay.v3.callback;

import com.google.gson.Gson;
import com.wangshouyu.mp.pay.common.utils.AesUtils;

/**
 * @author 王守钰
 * @program wsy-mp-pay
 * @date 2020年08月15日 01:15
 * @description: 支付回调工具
 */
public class PayCallbackUtils {

    private static final Gson GSON = new Gson();

    /**
     * 回调解密
     * @param callback 回调
     * @param aesKey 秘钥值
     * @return 解密数据
     */
    public static PayCallbackTransactions decrypt(PayCallback callback, String aesKey){
        PayCallbackResource resource = callback.getResource();

        try {
            String cert = AesUtils.decryptToString(
                    aesKey,
                    resource.getAssociatedData(),
                    resource.getNonce(),
                    resource.getCipherText());
            return GSON.fromJson(cert, PayCallbackTransactions.class);
        } catch (Exception e) {
            throw new PayCallbackException(e);
        }

    }
}
